package com.acane.instagram.controller;

import com.acane.instagram.pojo.Post;
import com.acane.instagram.pojo.Result;
import com.acane.instagram.service.PostService;
import com.acane.instagram.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    // 修改搜索接口，添加多条件参数
    @GetMapping("/search/list")
    public Result searchPosts(
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String nickName,
            @RequestParam(required = false) String content,
         //   @RequestParam(required = false) int status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        List<Post> posts = postService.searchPosts(userName, nickName, content,
               // status,
                startTime, endTime, page, size);
        return Result.success(posts, posts.size());
    }
    // 分页查询帖子
    @GetMapping("/get/page")
    public Result getPostListByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword
    ) {
        List<Post> postListByPage = postService.getPostListByPage(page, size);
        return Result.success(postListByPage, postListByPage.size());
    }

    @GetMapping("/get/list")
    public Result getPostList() {
        return Result.success(postService.getPostList());
    }

    @GetMapping("user/get")
    public Result getUserByPostId(@RequestParam String postId) {
        return Result.success(postService.getUserByPostId(postId));
    }
    @GetMapping("/recommend/list")
    public Result getRecommendPostList(@RequestParam String userId) {
        return Result.success(postService.getRecommendPostList(userId));
    }

    @GetMapping("/get/{userId}")
    public Result getPostListById(@PathVariable String userId) {
        return Result.success(postService.getPostListById(userId));
    }

    @PostMapping("/add")
    public Result addPost(@RequestBody Post post) {
        System.out.println("接收到的 post.shareRange: " + post.getShareRange());
        System.out.println("接收到的 post.showRange: " + post.getShowRange());
        post.setUserId(post.getUser().getId());
        post.setId(UUID.randomUUID().toString().replace("-", ""));
        post.setCreatedTime(LocalDateTime.now().toString());
        System.out.println("存入数据库的帖子信息：" + post);
        boolean save = postService.addPost(post);
        if (!save) {
            return Result.failure(400, "添加失败");
        }
        return Result.success(post);
    }

    @GetMapping("/get/one")
    public Result getPostById(@RequestParam String postId) {
        Post post = postService.getPostById(postId);
        return Result.success(post);
    }

    @PostMapping("/like")
    public Result likePost(@RequestParam String postId) {
        return Result.success(postService.likePost(postId));
    }

    @PostMapping("/unlike")
    public Result unlikePost(@RequestParam String postId) {
        return Result.success(postService.unlikePost(postId));
    }

    @GetMapping("/recommend/video")
    public Result getRecommendVideoList(@RequestParam String userId) {
        return Result.success(postService.getRecommendVideoList(userId));
    }

    @GetMapping("/search")
    public Result searchPost(@RequestParam String keyword) {
        return Result.success(postService.searchPost(keyword));
    }
}
